package com.uuah.server.config.spring.parsers.assembly;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.support.ManagedMap;

import com.uuah.server.config.spring.parsers.assembly.configuration.IPropertyConfiguration;
import com.uuah.utils.MapCombiner;

public abstract class AbstractMapBeanAssembler extends BeanAssembler {

	public AbstractMapBeanAssembler(IPropertyConfiguration beanConfig,
			BeanDefinitionBuilder bean, IPropertyConfiguration targetConfig,
			BeanDefinition target) {
		super(beanConfig, bean, targetConfig, target);
	}

	protected void insertDefinitionAsMap(String oldName) {
		BeanDefinitionBuilder builder = BeanDefinitionBuilder
				.rootBeanDefinition(MapCombiner.class);
		Map map = new ManagedMap();
		for (Iterator pvs = getBean().getBeanDefinition().getPropertyValues()
				.getPropertyValueList().iterator(); pvs.hasNext();) {
			PropertyValue pv = (PropertyValue) pvs.next();
			map.put(pv.getName(), pv.getValue());
		}
		List list = new ManagedList();
		list.add(map);
		builder.addPropertyValue(MapCombiner.LIST, list);
		setBean(builder);
		super.insertBeanInTarget(oldName);
	}

}